package com.its.myalgorithm.day04;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: dgl-algorithm
 * @package: com.its.myalgorithm.day04
 * @className: ListArrayUtils
 * @author: DONG GL
 * @description: TODO 链表生产器
 * @date: 2022/3/31 16:12
 * @version: 1.0
 */
public class ListArrayUtils {
    /**
     * 生产动态 链表
     * @param len
     * @param value
     * @return
     */
    public static Node generateRandomLinkedList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) {
            return null;
        }
        size --;
        Node head = new Node((int) ((Math.random() * (value + 1))));
        Node pre = head;
        while (size > 0) {
            Node cur = new Node((int) ((Math.random() * (value + 1))));
            pre.next = cur;
            pre = cur;
            size --;
        }
        return head;
    }

    /**
     * 双向链表
     * @param len
     * @param value
     * @return
     */
    public static DoubleNode generateRandomDoubleList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) {
            return null;
        }
        size--;
        DoubleNode head = new DoubleNode((int) (Math.random() * (value + 1)));
        DoubleNode pre = head;
        while (size != 0) {
            DoubleNode cur = new DoubleNode((int) (Math.random() * (value + 1)));
            pre.next = cur;
            cur.last = pre;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * 将链表 value
     * @param head
     * @return
     */
    public static List<Integer> getDoubleListOriginOrder(DoubleNode head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    /**
     * 单链表 value 顺序值
     * @param head
     * @return
     */
    public static List<Integer> getLinkedListOriginOrder(Node head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }
}
